package cn.tedu.sp06.filter;

import cn.tedu.sp01.web.util.JsonResult;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.apache.commons.lang.StringUtils;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Alex
 */
@Component
public class AccessFileter extends ZuulFilter {

    //设置过滤器的类型,pre,routing,post,error
    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    //设置过滤器的顺序号
    @Override
    public int filterOrder() {
        return 6;
    }

    //针对当前请求,是否要执行下面的过滤代码
    @Override
    public boolean shouldFilter() {
        /**
         * 请求商品需要判断权限
         * 请求其他模块,跳过权限判断
         */
        //获得请求上下文对象
        RequestContext ctx = RequestContext.getCurrentContext();
        //从上下文对象获得serviceId(正在调用的服务)
        String serviceId = (String)ctx.get(FilterConstants.SERVICE_ID_KEY);
        //serviceId是否是item-service
        return "item-service".equals(serviceId);
    }

    //过滤代码
    @Override
    public Object run() throws ZuulException {
        //http://localhost:3001/item-service/asahjgf?token=askdhakl
        //获取请求上下文
        RequestContext ctx = RequestContext.getCurrentContext();
        //从上下文对象获得reqeust对象
        HttpServletRequest request = ctx.getRequest();
        //从request中获得token参数
        String token = request.getParameter("token");
        //如果没有token,阻止继续调用,直接返回响应
        if(StringUtils.isBlank(token)){
            ctx.setSendZuulResponse(false); //阻止继续调用
            //告诉客户端返回的数据是什么类型
            ctx.addZuulResponseHeader("Content-Type","application/json;charset=UTF-8");
            String json = JsonResult.build().code(400).msg("未登录").toString();
        }
        //此返回值是无效的,直接返回null就行
        return null;
    }
}
